import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

class Solution {
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;
    public List<List<Integer>> permuteUnique(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        check = new boolean[nums.length];
        Arrays.sort(nums);
        dfs(nums);
        return ret;
    }
    public void dfs(int[] nums){
        if(path.size() == nums.length){
            ret.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i++){
            // 剪枝
            if(check[i] == true || (i != 0 && nums[i] == nums[i - 1] && check[i - 1] == false)){
                continue;
            }else{
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                // 回溯
                check[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }
}